// pages/login/authorize/authorize.js
let http = require('../../../utils/fetch.js')
var api = require('../../../utils/api.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    source:'',
    userInfo:[]
  },

  getPhoneNumber: function(e){
    var that = this;
    wx.login({
      success: function (event) {
        let mydata = {
          url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wxd21c3257f68ffeae&secret=096fe3cf56a04207525316519e58599a&js_code=' + event.code + '&grant_type=authorization_code',
          type: 'get'
        }        
        http.post(api.getCode, mydata).then((res) => {
          if (res.code == 0) {
            console.log("kkkk",res);
            that.postOpenid(JSON.parse(res.data).openid)
          } else {
            cosnole.log("请求失败")
          }

        })
      }
    })
    // if(e.detail.errMsg == 'getPhoneNumber:ok'){
    //   console.log(e)
    //   wx.setStorage({
    //     data: that.data.userInfo,
    //     key: 'userInfo',
    //   });
    //   wx.reLaunch({
    //     url: '../../'+that.data.source+'/'+that.data.source,
    //   })
    // } else {
    //   console.log(e);
    // }
  },
  getUser(e) { 
    console.log(e)
    wx.getUserInfo({
        success: (res) => {
            console.log(res)
            this.setData({
                userInfo: res.userInfo
            });
        }
    })
},
  postOpenid(openId) {
    var that = this;
    let mydata = {
      openid: openId
    }
    http.post(api.wxLogin, mydata).then((res) => {
      console.log(res)
      if (res.code == 0) {
        wx.setStorageSync('token', res.data.token)
        wx.setStorage({
          data: that.data.userInfo,
          key: 'userInfo',
        });
        if(res.data.CID == ''){
          wx.showModal({
            title: '申请',
            content: '您还没有小区，是否前往申请？',
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../../permission/permission',
                })
              } else if (res.cancel) {
                wx.reLaunch({
                  url: '../../index/index',
                })
              }
            }
          })
          
        } else {
          wx.reLaunch({
            url: '../../index/index',
          })
        }
      } else {
        cosnole.log("请求失败")
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getPhoneNumber()
    
    if (options) {
      this.setData({
        source: JSON.parse(options.obj).source,
        userInfo: JSON.parse(options.obj).userInfo
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})